extends CenterContainer


onready var name_input = $PanelContainer/VBoxContainer/name/LineEdit
onready var msic_op = $PanelContainer/VBoxContainer/msic_id/SearchSelect
onready var dt_op = $PanelContainer/VBoxContainer/dt/DTSelect
onready var bj_op = $PanelContainer/VBoxContainer/bj/BJSelect
onready var vs_music_input = $PanelContainer/VBoxContainer/vs_music/LineEdit
onready var vs_music_loop_offset_input = $PanelContainer/VBoxContainer/vs_music_loop_offset/SpinBox

var edit_data = null

var area_id



func set_area_id(area_id):
	self.area_id = area_id
	edit_data = DataSource.get_data_by_one(DataSource.map_sprite_area, area_id)
	var map_area_list = DataSource.get_data_list(DataSource.map_area)
	
	msic_op.set_data("选择音乐区域", map_area_list, "mapName")
	
	if edit_data:
		name_input.text = edit_data["areaDesc"]
		vs_music_input.text = edit_data.get("vsMusic", "")
		vs_music_loop_offset_input.value = edit_data.get("vsMusicLoopOffset", 0)
		
		msic_op.set_value(edit_data.get("msicId", -1))
		
		dt_op.select_value(edit_data["dt"]) 
		bj_op.select_value(edit_data["bj"]) 
		
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑精灵分布"
	else:
		pass
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	if edit_data:
		read_form(edit_data)
		
		if !edit_data["areaDesc"]: 
			MainTool.toast_info("名称不能为空!")
			return
		
		DataSource.update_data(DataSource.map_sprite_area, edit_data)
		
	else:
		var data = {
			"id": area_id,
			"mapId": position_info["map_register"]["id"]
		}
		
		read_form(data)
		
		if !data["areaDesc"]: 
			MainTool.toast_info("名称不能为空!")
			return
		
		DataSource.add_data(DataSource.map_sprite_area, data)
	
	MainTool.toast_info("更新区域信息成功!")
	
	map_view.sprite_area_update()
	
	MainTool.getUILayer().pop_ui()


func read_form(dic):
	dic["areaDesc"] = name_input.text
	dic["starProbability"] = 1
	dic["msicId"] = msic_op.get_value()
	dic["vsMusic"] = vs_music_input.text
	dic["vsMusicLoopOffset"] = vs_music_loop_offset_input.value
	dic["dt"] = dt_op.get_value()
	dic["bj"] = bj_op.get_value()

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
